import {Util} from "@lowcode/jpaas-common-lib";
import SelectSetting from "@/views/modules/form/bo/controlSetting/SelectSetting"

export default {
    data(){
        return {
            entityDrawerVisible:false,
            controlJson:{},
            idField:'id',
            isSingleInclud:['rx-checkbox-list','rx-radio','rx-form-select','rx-tree-select'],
            controls:[{label:'编辑型控件',value:'rx-button-edit'},
                {label:'复选框',value:'rx-checkbox'},
                {label:'复选框列表',value:'rx-checkbox-list'},
                {label:'日期控件',value:'rx-date'},
                {label:'日期范围控件',value:'rx-date-range'},
                {label:'部门控件',value:'rx-group'},
                {label:'月份控件',value:'rx-month'},
                {label:'数字控件',value:'rx-number'},
                {label:'单选',value:'rx-radio'},
                {label:'评分',value:'rx-rate'},
                {label:'富文本框',value:'rx-quill-editor'},
                {label:'下拉框',value:'rx-form-select'},
                {label:'开关',value:'rx-switch'},
                {label:'多行文本框',value:'rx-textarea'},
                {label:'文本控件',value:'rx-textbox'},
                {label:'时间控件',value:'rx-time'},
                {label:'上传控件',value:'rx-upload'},
                {label:'用户控件',value:'rx-user'},
                {label:'地址控件',value:'rx-address'},
                {label:'周控件',value:'rx-week'},
                {label:'下拉树',value:'rx-tree-select'},
                {label:'签名控件',value:'rx-form-signbox'},
                {label:'隐藏域',value:'rx-form-hidden'},
                {label:'动态文本框',value:'rx-dynamic-textbox'},
                {label:'关联流程',value:'rx-relate-process'},
                {label:'地区控件',value:'rx-district'},
                {label:'引用字段',value:'rx-ref'},
                {label:'标准字段',value:'rx-commonfield'},
                {label:'图片上传',value:'rx-image'},
                {label:'生成二维码',value:'rx-barcode'},
                {label:'单值编辑',value:'rx-button-edit-single'},
            ]
        }
    },
    methods:{
        i18n(name,text,key){
            return this.$ti18(name,text,"formCodegenBusiness",key);
        },
        setSetting(){
            var dataSource=null;
            var extJson={};
            if(this.controlJson && this.controlJson.extJson){
                if(typeof(this.controlJson.extJson)=='string'){
                    extJson=JSON.parse(this.controlJson.extJson);
                }
                else if(typeof(this.controlJson.extJson)=='object'){
                    extJson=this.controlJson.extJson;
                }
                if(extJson.dataSource){
                    dataSource=extJson.dataSource
                }
            }
            var self=this;
            Util.open({
                component: SelectSetting,
                curVm: this,
                zIndex:999999,
                widthHeight: ['1024px', '600px'],
                title: '数据来源配置',
                data: {
                    fields:this.fields,
                    dataSource:dataSource
                },
            },  function (action,data) {
                if(action!="ok") return;
                extJson.dataSource=data;
                self.controlJson.extJson=extJson;
            })
        },
        showDel(record) {
            return record.control!="rx-ref" &&
                ![this.mdl.idField,this.mdl.fkField].includes(record.fieldName);
        },
        showSetting(record){
            //表单生成的不显示
            if(!this.showEdit()){
                return false;
            }
            var  control = this.isSingleInclud.find((item) => {
                return item == record.control;
            })
            if(!control){
                return false;
            }
            return true;
        },
        showEdit(){
            return this.mdl.genMode && this.mdl.genMode!='form' && this.mdl.genMode!='create';
        },
        editRow(record) {
            this.entityDrawerVisible = true;
            //清空内容
            this.controlJson = {};
            this.controlJson = Util.deepClone(record);
            if (this.controlJson.extJson && typeof (this.controlJson.extJson) == "string") {
                this.controlJson.extJson = JSON.parse(this.controlJson.extJson);
            }
        },
        saveAttr() {
            if (!this.controlJson.name) {
                this.$notification["warning"]({
                    message: '字段属性提示',
                    description: "请输入字段名",
                });
                return;
            }
            if (!this.controlJson.control) {
                this.$notification["warning"]({
                    message: '字段属性提示',
                    description: "请输入控件类型",
                });
                return;
            }
            var json = Util.deepClone(this.controlJson);
            var rows = this.$refs.attrList.getData();
            for (var i = 0; i < rows.length; i++) {
                if (rows[i][this.idField] == json[this.idField]) {
                    this.$refs.attrList.updateRow(rows[i], json);
                }
            }
            this.entityDrawerVisible = false;
        },
        //关闭抽屉
        onClose() {
            this.entityDrawerVisible = false;
        }
    }
}